package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.SearchResult;
import com.elinkway.infinitemovies.bean.SearchResultList;
import com.elinkway.infinitemovies.bean.SearchResultType;

import org.json.JSONArray;
import org.json.JSONObject;

public class SearchResultParser extends LVideoMobileParser<SearchResultList> {

	private static final String DATALIST="data";
	private static final String NAME = "name";
	private static final String RATING = "rating";
	private static final String SUBCATEGORY = "subcategory";
	private static final String EPISODES = "episodes";
	private static final String SUBCATEGORYNAME = "subcategoryname";
	private static final String VIDEOTYPENAME = "videotypename";
	private static final String CATEGORY = "category";
	private static final String CATEGORYNAME = "categoryname";
	private static final String STARRINGNAME = "starringname";
	private static final String ISEND = "isend";
	private static final String SCORE = "score";
	private static final String SUBSRC = "subsrc";
	private static final String NOWEPISODES = "nowepisodes";
	private static final String AREANAME = "areaname";
	private static final String POSTER = "poster";
	private static final String AID = "aid";
	private static final String SRC = "src";
	private static final String AREA = "area";
	private static final String RELEASEDATE = "releasedate";
	private static final String SUBNAME = "subname";
	private static final String DIRECTORY = "directory";
	private static final String VT = "vt";
	private static final String GLOBAL_ID = "global_id";
	private static final String TOTALRECORDS="totalrecords";
	private static final String CHANNELCOUNT="channelcount";
	private static final String CHANNEL_NAME= "channelname";
	private static final String CHANNEL_VT="vt";
	private static final String RECORDS="records";
	@Override
	public SearchResultList parse(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		SearchResultList list = new SearchResultList();
		if(data.has(TOTALRECORDS)){
			list.setTotalRecords(data.getInt(TOTALRECORDS));
		}
		list.setEid(data.optString("eid"));
		list.setExperimentStr(data.optString("experiment_str"));
		list.setTriggerStr(data.optString("trigger_str"));

		JSONArray channel_arr = data.optJSONArray(CHANNELCOUNT);
		for(int i = 0 ;i<channel_arr.length();i++) {
			SearchResultType searchType = new SearchResultType();
			JSONObject channel_obj = channel_arr.optJSONObject(i);
			searchType.setChannelname(channel_obj.optString(CHANNEL_NAME));
			searchType.setRecords(channel_obj.optString(RECORDS));
			searchType.setVt(channel_obj.optString(CHANNEL_VT));
			list.getResultType().add(searchType);
		}
		if(data.has(DATALIST)){
			JSONArray arr = data.getJSONArray(DATALIST);
			for(int i =0;i<arr.length();i++){
				JSONObject obj = arr.getJSONObject(i);
				SearchResult item = new SearchResult();
				if (obj.has(RATING)){
				    item.setRating(obj.getString(RATING));
				    }
				if (obj.has(SUBCATEGORY)){
				    item.setSubcategory(obj.getString(SUBCATEGORY));
				    }
				if (obj.has(EPISODES)){
				    item.setEpisodes(obj.getString(EPISODES));
				    }
				if (obj.has(SUBCATEGORYNAME)){
				    item.setSubcategoryname(obj.getString(SUBCATEGORYNAME));
				    }
				if (obj.has(VIDEOTYPENAME)){
				    item.setVideotypename(obj.getString(VIDEOTYPENAME));
				    }
				if (obj.has(CATEGORY)){
				    item.setCategory(obj.getString(CATEGORY));
				    }
				if (obj.has(CATEGORYNAME)){
				    item.setCategoryname(obj.getString(CATEGORYNAME));
				    }
				if (obj.has(STARRINGNAME)){
				    item.setStarringname(obj.getString(STARRINGNAME));
				    }
				if (obj.has(ISEND)){
				    item.setIsend(obj.getString(ISEND));
				    }
				if (obj.has(SUBSRC)){
				    item.setSubsrc(obj.getString(SUBSRC));
				    }
				if (obj.has(NOWEPISODES)){
				    item.setNowepisodes(obj.getString(NOWEPISODES));
				    }
				if (obj.has(AREANAME)){
				    item.setAreaname(obj.getString(AREANAME));
				    }
				if (obj.has(POSTER)){
				    item.setPoster(obj.getString(POSTER));
				    }
				if (obj.has(AID)){
				    item.setAid(obj.getString(AID));
				    }
				if (obj.has(SRC)){
				    item.setSrc(obj.getString(SRC));
				    }
				if (obj.has(NAME)){
				    item.setName(obj.getString(NAME));
				    }
				if (obj.has(AREA)){
				    item.setArea(obj.getString(AREA));
				    }
				if (obj.has(RELEASEDATE)){
				    item.setReleasedate(obj.getString(RELEASEDATE));
				    }
				if (obj.has(SUBNAME)){
				    item.setSubname(obj.getString(SUBNAME));
				    }
				if (obj.has(DIRECTORY)){
				    item.setDirectory(obj.getString(DIRECTORY));
				    }
				if (obj.has(VT)){
				    item.setVt(obj.getString(VT));
				    }
				item.setGlobalId(obj.optString(GLOBAL_ID));
				list.getResultList().add(item);
			}
		}
		return list;
	}

}
